#pragma once 
#include<iostream>
#include"Log.hpp"
#include<fstream>
#include<string>
#include<unordered_map>
namespace dict_ns
{
    const std::string sep = ": ";
    const std::string defaultpath = "./Dict.txt";
    class Dict
    {
        private:
            std::unordered_map<std::string,std::string> _dict;
            std::string _dict_filepath;
        private:
            bool Load()
            {
                std::ifstream in(_dict_filepath);
                if(!in.is_open())
                {
                    LOG(FATAL, "open %s error\n", defaultpath.c_str());
                    return false;
                }
                std::string line;
                while(std::getline(in,line))
                {
                    if(line.empty())
                        continue;
                    auto pos = line.find(sep);
                    if(pos == std::string::npos) 
                        continue;
                    std::string word = line.substr(0,pos);
                    if(word.empty())
                        continue;
                    std::string chinese = line.substr(pos+sep.size());
                    if(chinese.empty())
                        continue;
                    LOG(INFO,"load info, %s : %s",word.c_str(),chinese.c_str());
                    _dict.insert(std::make_pair(word,chinese));
                }
                in.close();
                LOG(INFO,"load %s success",_dict_filepath.c_str());
                return true;
            }
        public:
            Dict(const std::string& path = defaultpath)
            :_dict_filepath(path)
            {
                Load();
            }
            std::string Translate(const std::string& word,bool& ok)
            {
                ok = true;
                auto iter  = _dict.find(word);
                if(iter == _dict.end())
                {
                    ok = false;
                    return "未找到";
                }
                return iter->second;
            }
            ~Dict()
            {}
    };
}